package com.example.zg.androidnewview.customView;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

/**
 * 创建日期:2019/1/15 on 11:25
 * 描述：自定义RecyclerView分割线 下划线
 * 作者: zhougan
 */
public class ItemDecorationLine extends RecyclerView.ItemDecoration {
    private static final String TAG = "ItemDecorationLine";
    private int divideHeight=2;//分割线高度
    private int divideColor= Color.GRAY;//分割线颜色
    private Paint mPaint;

    private void initPaint(){
        if(mPaint==null){
            mPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
            mPaint.setColor(divideColor);
            mPaint.setStrokeWidth(1);
        }
    }

    /**
     * 设置分割线高度
     * @param divideHeight
     */
    public ItemDecorationLine setDivideHeight(int divideHeight) {
        this.divideHeight = divideHeight;
        return this;
    }

    /**
     * 设置分割线颜色
     * @param divideColor
     */
    public ItemDecorationLine setDivideColor(int divideColor) {
        this.divideColor = divideColor;
        return this;
    }

    //在Item绘制之前被调用，该方法主要用于绘制间隔样式
    //重写次方法即可绘制
    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        initPaint();
        float left=parent.getPaddingLeft();//在parent上的left位置
        float right=parent.getRight()-parent.getPaddingRight();
        int childCount=parent.getChildCount();
        for (int i=0;i<childCount;i++){
            View child=parent.getChildAt(i);
            RecyclerView.LayoutParams params= (RecyclerView.LayoutParams) child.getLayoutParams();
            float top=child.getBottom()+params.bottomMargin+ Math.round(child.getTranslationY());
            float bottom=top+divideHeight;
            c.drawRect(left,top,right,bottom,mPaint);
        }



    }

    //设置item的偏移量(Item四周的间隔)，偏移的部分用于填充间隔样式，即设置分割线的宽、高
    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {

        outRect.set(0,0,0,divideHeight);
    }

    //在Item绘制之后被调用，该方法主要用于绘制间隔样式
    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {


    }



}
